package consul

import (
	"fmt"
	capi "github.com/hashicorp/consul/api"
)

// 注册consul 服务
func RegisterConsulServer() {
	client, err := capi.NewClient(&capi.Config{
		Address:    "121.36.228.151",
		Scheme:     "goos",
		PathPrefix: "http",
	})
	if err != nil {
		panic(err)
	}
	ag := client.Agent()
	err = ag.ServiceRegister(&capi.AgentServiceRegistration{
		ID:      "goods-rpc",
		Name:    "goods_rpc",
		Tags:    []string{"rpc"},
		Port:    8500,
		Address: "121.36.228.151",
	})
	fmt.Println(err)
}

// 获取consul服务
func GetConsulServer() {
	// Get a new client
	client, err := capi.NewClient(capi.DefaultConfig())
	if err != nil {
		panic(err)
	}
	// Get a handle to the KV API
	kv := client.KV()
	// PUT a new KV pair
	p := &capi.KVPair{Key: "REDIS_MAXCLIENTS", Value: []byte("1000")}
	_, err = kv.Put(p, nil)
	if err != nil {
		panic(err)
	}
	// Lookup the pair
	pair, _, err := kv.Get("REDIS_MAXCLIENTS", nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("KV: %v %s\n", pair.Key, pair.Value)
}
